<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:fn="fn" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:gx="http://www.google.com/kml/ext/2.2"
	version="2.0"
	exclude-result-prefixes="xs fn">
	<xsl:output indent="yes" encoding="UTF-8" />
	<xsl:template match="/mission" name="main">
		<!--New form to permit for gx has replaced  <kml xmlns="http://www.opengis.net/kml/2.2">-->
		<kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2" xmlns:kml="http://www.opengis.net/kml/2.2">
			<Document>
                <open>1</open>
				<name>SAUV Path</name>
				<description>Example of SAUV Path generation from CSV.</description>
				
				<!--Styles -->
                <Style id="sauv">
                    <IconStyle>
                        <Icon>
                            <href>http://maps.google.com/mapfiles/kml/shapes/airports.png</href>
                        </Icon>
                        <hotSpot y="0" x="0.5" xunits="fraction" yunits="fraction"/>
                    </IconStyle>
                    <LabelStyle>
                        <scale>0</scale>
                    </LabelStyle>
                    <LineStyle>
                        <color>cc00ffff</color>
                        <width>3</width>
                    </LineStyle>
                </Style>
                
                <!-- Schema definition for data -->
                 <!-- Declare the type "SAUVType" with fields for each reported sensor -->
                <Schema id="SAUVDataTypeId"> 
                    <gx:SimpleArrayField name="DM.recNum" type="int">
                        <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="DM.UTC" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="DM.date" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="DM.time" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="DM.recVer" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="MM.newRec" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="MM.time" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="MM.state" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="MM.legId" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="MM.miss.maneuv" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="MM.curCCL" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="MM.curLLC" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="MM.ssStats" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.newRec" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.V" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.Ah" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.Wh" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.avail" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.time" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.type" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.V" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.Ah" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.Wh" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.delta Wh" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.recUse Wh" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.eLim Wh" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.tLim sec" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.eeCtr" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="EM.eu.elCtr" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.newRec" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.status" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.firmVer" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.atRate mA" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.atRateTm2Full min" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.atRateTm2Empty min" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.voltage mV" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.current mA" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.aveCrnt mA" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.relStateOfChg %" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.absStateOfChg %" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.remCap mAh" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.fullChgCap mAh." type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.runTm2Empty min" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.aveTm2Empty min" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.aveTm2Full min" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.chgAmps mA" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.chgVolts mV" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.packVolts mV" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.aveVolts mV" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="ES.solVolts mV" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.newRec" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.navTm" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.status" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.depth" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.heading" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.pitch" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.roll" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.alt" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.speed" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.spdOvrGnd" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.cond" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.temp" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.GPSLat" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.GPSLon" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.validPos" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commLat" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commLon" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commN" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commE" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commDepth" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commHeading" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commPitch" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commAlt" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commSpeed" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.commThrust" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.cmpLat" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.cmpLon" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.cmpN" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.cmpE" type="float">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.DO2" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.ADChan2" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.ADChan3" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.ADChan4" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="Nav.ADChan5" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.mode" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.fault" type="string">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.thrust" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.current" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.vPos" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.hPos" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.commVPos" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.commHPos" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.D1" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="PMC.D2" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                    <gx:SimpleArrayField name="line length" type="int">
	                    <displayName><![CDATA[]]></displayName>
                    </gx:SimpleArrayField>
                </Schema>
                
                <!-- The GPS Track method of recording this data 
                Placemark ->
                    gx:Track ->
                        when
                        coord
                
                -->
    <Camera>
        <gx:TimeSpan>
        <begin><xsl:call-template name="firstPointTime"/></begin>
        <end><xsl:call-template name="lastPointTime"/></end>
        </gx:TimeSpan>
        <xsl:call-template name="initCamPos"/>
        
        <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
    </Camera>
                <Placemark id="pl">
                    <name>SAUV Track</name>
                    <description>SAUV Record</description>
                    <styleUrl>#sauv</styleUrl>
                    <gx:Track id="electrictrack">
                        <altitudeMode>absolute</altitudeMode><xsl:text>
                        </xsl:text>
                        <xsl:apply-templates select="point"/><xsl:text>
                        </xsl:text>
                        <ExtendedData>
                            <SchemaData schemaUrl="#SAUVDataTypeId">
                                <gx:SimpleArrayData name="EM.V">
                                    <xsl:apply-templates select="point/EM.V"/>
                                </gx:SimpleArrayData>
                                <gx:SimpleArrayData name="EM.Ah">
                                    <xsl:apply-templates select="point/EM.Ah"/>
                                </gx:SimpleArrayData>
                                <gx:SimpleArrayData name="EM.Wh">
                                    <xsl:apply-templates select="point/EM.Wh"/>
                                </gx:SimpleArrayData>
                                <gx:SimpleArrayData name="EM.avail">
                                    <xsl:apply-templates select="point/EM.avail"/>
                                </gx:SimpleArrayData>
                            </SchemaData>
                        </ExtendedData>
                        <Model>
                        <Link>
                        <href>http://earth-outreach.googlecode.com/svn/trunk/kml_lessons/morekml/advanced/lesson_12/cessna.dae</href>
                        </Link>
                        <Orientation>
                        <heading>180</heading>
                        </Orientation>
                        </Model>
                    </gx:Track>
                </Placemark>
			</Document>
		</kml>
	</xsl:template>
	<!-- Select Elements to Copy <root> <row> <elem name="" DM.recNum DM.UTC 
		MM.legId MM.curCCL Nav.depth Nav.heading Nav.GPSLat Nav.GPSLon -->
	<!-- DM.recNum 1650 - end 1st sample 1770 - begin 2nd sample 2494 - end 
		2nd sample -->
	<xsl:template match="point">
	        <!--must generate dateTime (YYYY-MM-DDThh:mm:sszzzzzz) from 03/26/2012 11:44:47 in EST-->
	        <xsl:variable name="dateArray" select="tokenize(DM.date, '/')"/>
	        <xsl:variable name="whenDate" select="concat($dateArray[3], '-', $dateArray[1], '-', $dateArray[2])"/>
	        <when><xsl:value-of select="$whenDate"/><xsl:text>T</xsl:text><xsl:value-of select="substring-before(DM.time, '.')" />-05:00</when>
            <gx:coord><xsl:value-of select="Nav.GPSLon" /><xsl:text> </xsl:text><xsl:value-of select="Nav.GPSLat" /><xsl:text> </xsl:text><xsl:value-of select="Nav.alt" /></gx:coord>
	</xsl:template>
	
	<xsl:template name="firstPointTime">
	        <xsl:variable name="dateArray" select="tokenize(DM.date, '/')"/>
	        <xsl:variable name="whenDate" select="concat($dateArray[3], '-', $dateArray[1], '-', $dateArray[2])"/><xsl:value-of select="$whenDate"/><xsl:text>T</xsl:text><xsl:value-of select="substring-before(DM.time, '.')" /><xsl:text>-05:00</xsl:text>
	</xsl:template>

	<xsl:template name="lastPointTime">
	    <xsl:variable name="dateArray" select="tokenize(DM.date, '/')"/>
	        <xsl:variable name="whenDate" select="concat($dateArray[3], '-', $dateArray[1], '-', $dateArray[2])"/><xsl:value-of select="$whenDate"/><xsl:text>T</xsl:text><xsl:value-of select="substring-before(DM.time, '.')" /><xsl:text>-05:00</xsl:text>
	</xsl:template>
		
	<xsl:template name="initCamPos">
	    <longitude><xsl:value-of select="point[1]/Nav.GPSLon - .01"/></longitude>
        <latitude><xsl:value-of select="point[1]/Nav.GPSLat - .01"/></latitude>
        <altitude><xsl:value-of select="point[1]/Nav.alt + 250"/></altitude>
        <heading><xsl:value-of select="point[1]/Nav.heading"/></heading>
        <tilt>45</tilt>
        <roll>0</roll>
	</xsl:template>
	
    <xsl:template match="point/EM.V">
        <gx:value><xsl:value-of select="."/></gx:value>
    </xsl:template>
    <xsl:template match="point/EM.Ah">
        <gx:value><xsl:value-of select="."/></gx:value>
    </xsl:template>
    <xsl:template match="point/EM.Wh">
        <gx:value><xsl:value-of select="."/></gx:value>
    </xsl:template>
    <xsl:template match="point/EM.avail">
        <gx:value><xsl:value-of select="."/></gx:value>
    </xsl:template>
</xsl:stylesheet>
